/*
 * @Author: 高富强
 * @Email: 875627570@qq.com
 * @Date: 2020-06-18 08:24:40
 * @Last Modified by: 高富强
 * @Last Modified time: 2020-06-19 14:24:40
 * @Description: 备课音频页
 */
import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_code_framework/base/base_widget.dart';
import 'package:flutter_code_framework/utils/global.dart';
import 'package:flutter_code_framework/widgets/voice_player_circle_widget.dart';

import 'package:flutter_plugin_record/flutter_plugin_record.dart';
import 'package:flutter_code_framework/widgets/voice_widget.dart';
import 'package:flutter_sfsymbols/flutter_sfsymbols.dart';
import 'package:flutter_code_framework/route/route_transition.dart';
import 'package:flutter_code_framework/utils/global.dart';
import 'package:flutter_code_framework/model_data/events/base_event.dart';
import 'lesson_publish_event.dart';

class LessonAudioPage extends StatefulWidget {
  final Map args;

  LessonAudioPage({this.args});

  @override
  _LessonAudioPageState createState() =>  _LessonAudioPageState();

}

class _LessonAudioPageState extends State<LessonAudioPage> {
  StreamSubscription _streamSubscription;
  Map args;
  FlutterPluginRecord recordPlugin = new FlutterPluginRecord();
  String  filePath ="";
  int audioTimeLength=0;
  Color _ibColor=Colors.grey;
  //Color _ibColor=Theme.of(context).primaryTextTheme.caption.color;
  bool _isPlay=false;
  int  _audioTimeLength=0;
  //Icon _iconPlay=new Icon(SFSymbols.play_fill,size: 100,color: _ibColor);

  Function recoverFilePath()  {
    setState(() {
      filePath="";
      _ibColor=Colors.grey;
      //_ibColor=Theme.of(context).primaryTextTheme.caption.color;
      _isPlay=false;
      _audioTimeLength=0;
    });
    //Navigator.of(context).pop();
  }
  startRecord(){
    print("111开始录制");
  }
  ///播放语音的方法
  void play() {
    //recordPlugin.play();
    print("开始播放");
    recordPlugin.playByPath(filePath);
  }
  stopRecord(String path,double audioTimeLength ){
    print("结束束录制");
    print("音频文件位置"+path);

    setState(() {
      filePath=path;
      //_ibColor=Colors.green;
      _ibColor= Theme.of(context).primaryTextTheme.button.color;
      _isPlay=true;
      _audioTimeLength=audioTimeLength.floor()-1;
    });
    print("音频录制时长"+audioTimeLength.toString());
  }

  @override
  Widget build(BuildContext context) {
    //print('页面加载时的filePath:'+filePath);
    if(filePath!='') {
      G.eventBus.fire(LessonPublishEvent(audioPath:filePath,audioTimeLength:_audioTimeLength));
      //发送订阅消息给父页面
    }
    return
      Container(
        color:Color.fromRGBO(232,232,232,0.6),
        alignment: Alignment.center,
        width: MediaQuery.of(context).size.width,
        height: MediaQuery.of(context).size.height* 0.7,
        child: Column(
          children: <Widget>[
            Expanded(
              flex:3,
              child:Column(
                children: <Widget>[
                  Container(
                    height:200,
                    width:150,
                    decoration: null,//Circle
                    child:_isPlay?
                    VoicePlayerCircleWidget(url: filePath,req_seconds:_audioTimeLength):
                    IconButton(
                      icon:  Icon(SFSymbols.play_circle_fill,size: 90,color: _ibColor,),
                      onPressed: () {
                      },
                      alignment: Alignment.bottomCenter,
                      tooltip:"播放",
                    ),
                  ),
                  Container(
                    child: _isPlay?null:Text('0:00:00',style: TextStyle(color: Color.fromRGBO(118,118,118,1),fontSize: 18),),
                  ),],
              ),
            ),
            Expanded(
              flex:3,
              child:
              Container(
                height: 90.0,
                child:
                Row(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    children: <Widget>[
                      _isPlay?
                      GestureDetector(
                        onTap: (){
                          G.showToastDialog(context,
                              "再次录制会覆盖上一段音频，是否重新录制？",
                              title: "提示", callBack: recoverFilePath);
                        },
                        child:
                        Container(
                          height: 90,
                          margin: EdgeInsets.fromLTRB(50, 0, 50, 20),
                          child: Center(
                            child: Row(
                              children: <Widget>[
                                Icon(SFSymbols.mic_circle_fill,size:90,
                                  color:
                                  Theme.of(context).accentColor
                                //Color.fromRGBO(236,115,100,1)
                                  ,
                                ),
                              ],
                            ),
                          ),
                        ),
                      ):
                      VoiceWidget(startRecord: startRecord,stopRecord: stopRecord),
                    ]
                ),
              ),
            ),
          ],
        ),
    );
  }

  @override
  void initState() {
    super.initState();

    setState(() {
      args = widget.args;
      if(args!=null&&args["audioPath"]!=null){
            filePath=args["audioPath"];
            //_ibColor= Theme.of(context).primaryTextTheme.button.color;
            _isPlay=true;
            _audioTimeLength=args["audioTimeLength"];
            //print('列表页传值audioPath:'+filePath);
      }
    });
    _streamSubscription = G.eventBus.on<RecordingAudioPathEvent>().listen((event) {
      filePath=event.audioPath;
      audioTimeLength=event.audioTimeLength;
    });
  }

}
